import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Solution {
    public int totalFruit(int[] fruits) {
        Map<Integer,Integer> hash = new HashMap<>();//统计窗口内的水果种类
        int ret = 0;
        for (int left = 0,right = 0;right < fruits.length;right++){
            int in = fruits[right];
            hash.put(in,hash.getOrDefault(in,0)+1);//进窗口
            while(hash.size() > 2){
                int out = fruits[left];
                hash.put(out,hash.get(out)-1);//出窗口
                if(hash.get(out) == 0){
                    hash.remove(out);
                }
                left++;
            }
            ret = Math.max(ret,right-left+1);
        }
        return ret;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNextInt()){
            int size = in.nextInt();
            int[] fruits = new int[size];
            for (int i = 0; i < size; i++) {
                fruits[i] = in.nextInt();
            }
            Solution solution = new Solution();
            System.out.println(solution.totalFruit(fruits));
        }
    }
}
